package com.hb.game.pk.adpter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.utils.LogUtils;
import com.hb.game.pk.utils.SpannableStringUtils;
import com.hb.game.pk.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 弃用
public class ChatListAdapter1 extends RecyclerView.Adapter<ChatListAdapter1.ViewHolder> {
    private Activity act;
    private List<Map> data;
    private OnClickItemListener onClickItemListener;
    private Map<String, Boolean> clicked = new HashMap<>();
    private int type;

    public ChatListAdapter1(Activity act) {
        this.act = act;
    }

    public void setNewData(List<Map> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addMore(Map map) {
        this.data.add(map);
        notifyItemInserted(data.size());
//        compatibilityDataSizeChanged(1);
//        notifyDataSetChanged();
        notifyItemRangeChanged(data.size(), data.size() - data.size());
    }

    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = data == null ? 0 : data.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<Map> getData() {
        return this.data;
    }

    @Override
    public ChatListAdapter1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(act, R.layout.item_chat_list_2, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads);
        } else {
            Map item = data.get(position);
            if ((item.containsKey("m") && Float.parseFloat(item.get("m").toString()) == 1.0f) || !item.containsKey("m")) {
                LogUtils.d("item.containsKey_____ " + item.containsKey("cliced"));
                if (type != 1) {
                    if (!item.containsKey("cliced")) {
                        holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                        holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                        holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left);
                        holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right);
                    } else {
                        holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                        holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                        holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left_clicked);
                        holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right_clicked);
                    }
                } else {
                    if (!item.containsKey("cliced")) {
                        holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                        holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                        holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left);
                        holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right);
                    } else {
                        holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                        holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                        holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left_clicked);
                        holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right_cliced);
                    }
                }
            }
        }
    }

    @Override
    public void onBindViewHolder(final ChatListAdapter1.ViewHolder holder, final int position) {
        Map item = data.get(position);
        holder.rlLeft.setVisibility(View.GONE);
        holder.rlRight.setVisibility(View.GONE);
        holder.tvLq.setVisibility(View.GONE);
        holder.tvZj.setVisibility(View.GONE);
        if ((item.containsKey("m") && Float.parseFloat(item.get("m").toString()) == 1.0f) || !item.containsKey("m")) {
            String username = "";
            String money = "";
            int res = 0;
            if (type != 1) {
                if (!item.containsKey("cliced")) {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_normal);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right);
                } else {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_blue_clicked);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_blue_left_clicked);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_blue_right_clicked);
                }
            } else {
                if (!item.containsKey("cliced")) {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_normal);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right);
                } else {
                    holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                    holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                    holder.ivLeftLeft.setBackgroundResource(R.mipmap.ic_chat_left_clicked);
                    holder.ivRightRight.setBackgroundResource(R.mipmap.ic_chat_right_cliced);
                }
            }
            if (!item.containsKey("m")) {
                String trim = StringUtils.getString(item.get("avatar").toString());
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                username = item.get("username").toString();
                money = String.format("%.2f", Float.parseFloat(item.get("money").toString()) / 100);
                if (APP.userInfo.getUser_id().equals(item.get("user_id").toString())) {
                    holder.rlRight.setVisibility(View.VISIBLE);
                    holder.ivHeadRight.setBackgroundResource(res);
                    holder.tvNameRight.setText(username);
                    if (!"0".equals(item.get("user_id").toString()))
                        holder.tvMoneyRight.setText(money + "-" + item.get("bom_num").toString());
                    else
                        holder.tvMoneyRight.setText("系统发包");
                } else {
                    holder.rlLeft.setVisibility(View.VISIBLE);
                    holder.ivHeadLeft.setBackgroundResource(res);
                    holder.tvNameLeft.setText(username);
                    if (!"0".equals(item.get("user_id").toString()))
                        holder.tvMoneyLeft.setText(money + "-" + item.get("bom_num").toString());
                    else
                        holder.tvMoneyLeft.setText("系统发包");
                }
            } else {
                Map<String, Object> data = (Map<String, Object>) item.get("data");
                String trim = StringUtils.getString(data.get("avatar").toString());
                if (StringUtils.isEmpty(trim)) {
                    res = R.mipmap.ic_head_normal;
                } else {
                    int i = Integer.parseInt(trim);
                    res = APP.headResource.get(i - 1);
                }
                username = data.get("username").toString();
                money = String.format("%.2f", Float.parseFloat(data.get("money").toString()) / 100);
                if (APP.userInfo.getUser_id().equals(data.get("user_id").toString())) {
                    holder.rlRight.setVisibility(View.VISIBLE);
                    holder.ivHeadRight.setBackgroundResource(res);
                    holder.tvNameRight.setText(username);
                    if (!"0".equals(data.get("user_id").toString()))
                        holder.tvMoneyRight.setText(money + "-" + data.get("bom_num").toString());
                    else
                        holder.tvMoneyRight.setText("系统发包");
                } else {
                    holder.rlLeft.setVisibility(View.VISIBLE);
                    holder.ivHeadLeft.setBackgroundResource(res);
                    holder.tvNameLeft.setText(username);
                    if (!"0".equals(data.get("user_id").toString())) {
                        if (data.get("bom_num") == null) {
                            holder.tvMoneyLeft.setText(money);
                        } else {
                            holder.tvMoneyLeft.setText(money + "-" + data.get("bom_num").toString());
                        }
                    } else
                        holder.tvMoneyLeft.setText("系统发包");
                }
            }
        } else if (item.containsKey("m") && Float.parseFloat(item.get("m").toString()) == 2.0f) {
            holder.tvZj.setVisibility(View.VISIBLE);
            Map<String, Object> data = (Map<String, Object>) item.get("data");
            String name = data.get("username").toString();
            float money = Float.parseFloat(data.get("aword").toString());
            holder.tvZj.setText(SpannableStringUtils.getBuilder(name)
                    .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .append("中奖").setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                    .append((money / 100) + "元").setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .create());
//            holder.tvZj.setText(name + "中奖" + (money / 100) + "元");
        } else {
            holder.tvLq.setVisibility(View.VISIBLE);
            Map<String, Object> data = (Map<String, Object>) item.get("data");
            String name = data.get("username").toString();
            holder.tvLq.setText(SpannableStringUtils.getBuilder(name)
                    .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                    .append("领取了您的红包").setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                    .create());
//            holder.tvLq.setText(name + "领取了您的红包");
        }
        holder.rlLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null)
                    onClickItemListener.clickItemListener(ChatListAdapter1.this, holder, position);
            }
        });
        holder.rlRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null)
                    onClickItemListener.clickItemListener(ChatListAdapter1.this, holder, position);
            }
        });
        holder.tvLq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null)
                    onClickItemListener.clickItemListener(ChatListAdapter1.this, holder, position);
            }
        });
        holder.tvZj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickItemListener != null)
                    onClickItemListener.clickItemListener(ChatListAdapter1.this, holder, position);
            }
        });
    }

    public interface OnClickItemListener {
        public void clickItemListener(ChatListAdapter1 adapter, ChatListAdapter1.ViewHolder holder, int position);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public void setStatus(int position) {
        Map map = data.get(position);
        map.put("cliced", true);
        data.set(position, map);
        notifyItemChanged(position, "test");
//        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llRedBoxLeft;
        private LinearLayout llRedBoxRight;
        private ImageView ivRightRight;
        private ImageView ivLeftLeft;
        private TextView tvLq;
        private TextView tvZj;
        private TextView tvMoneyLeft;
        private TextView tvNameLeft;
        private ImageView ivHeadLeft;
        private RelativeLayout rlLeft;
        private TextView tvMoneyRight;
        private TextView tvNameRight;
        private ImageView ivHeadRight;
        private RelativeLayout rlRight;

        public ViewHolder(View view) {
            super(view);
            rlLeft = (RelativeLayout) view.findViewById(R.id.rl_left);
            ivHeadLeft = (ImageView) view.findViewById(R.id.iv_head_left);
            tvNameLeft = (TextView) view.findViewById(R.id.tv_name_left);
            tvMoneyLeft = (TextView) view.findViewById(R.id.tv_money_left);
            rlRight = (RelativeLayout) view.findViewById(R.id.rl_right);
            ivHeadRight = (ImageView) view.findViewById(R.id.iv_head_right);
            tvNameRight = (TextView) view.findViewById(R.id.tv_name_right);
            tvMoneyRight = (TextView) view.findViewById(R.id.tv_money_right);
            tvZj = (TextView) view.findViewById(R.id.tv_zj);
            tvLq = (TextView) view.findViewById(R.id.tv_lq);
            ivLeftLeft = (ImageView) view.findViewById(R.id.iv_left_left);
            ivRightRight = (ImageView) view.findViewById(R.id.iv_right_right);
            llRedBoxRight = (LinearLayout) view.findViewById(R.id.ll_red_box_right);
            llRedBoxLeft = (LinearLayout) view.findViewById(R.id.ll_red_box_left);
        }
    }
}
